<!--********************************************************************
* Copyright© 2000 - 2018 SuperMap Software Co.Ltd. All rights reserved.
*********************************************************************-->
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title data-i18n="resources.title_heatMapColorManual"></title>
    <style type="text/css">
        body {
            margin: 0;
            overflow: hidden;
            background: #fff;
            width: 100%;
            height: 100%
        }

        #map {
            position: absolute;
            width: 100%;
            height: 100%;
            border: 1px solid #3473b7;
        }

        #toolbar {
            position: absolute;
            top: 50px;
            right: 10px;
            width: 300px;
            text-align: center;
            z-index: 100;
            border-radius: 4px;
        }

        .row {
            position: absolute;
            right: 25px;
            top: 300px;
            background: #FFF;
            z-index: 100;
            border-top-width: 0px;
            width: 150px;
            display: none;
        }

        .winTitle {
            background: #1E90FF;
            font-size: 15px;
        }

        .input-group {
            margin-bottom: 5px;
        }
    </style>
</head>
<body>
<div id="toolbar" class="panel panel-primary" data-i18n="[style]resources.style_heatMapWidth">
    <div class='panel-heading'>
        <h5 class='panel-title text-center' data-i18n="resources.text_heatMapColorManual"></h5></div>
    <div class='panel-body content'>
        <div class='panel'>
            <div class='input-group'>
                <span class='input-group-addon' data-i18n="resources.text_countsDraw"></span>
                <input class='form-control' type='text' style='width:50px;' id='heatNums' value='200'/>
            </div>

            <div class='input-group'>
                <span class='input-group-addon' data-i18n="resources.text_radius"></span>
                <input class='form-control' type='text' style='width:50px' value='50' id='heatradius'/>
                <select class='form-control' style='width:70px' id='radiusUnit'>
                    <option value='px'>px</option>
                    <option value='degree'>degree</option>
                </select>
            </div>
            <div class='input-group'>
                <span class='input-group-addon' style="width:112px;height: 16px;"
                      data-i18n="resources.text_inputColor"></span>
                <input type="text" class='form-control' id="cp1text" value="" style=" width:102px;"/>
                <span class='input-group-addon'><img src='./images/colorpicker.png' id="cp1"
                                                     style="cursor:pointer"/></span>
            </div>
        </div>
        <input type="button" class="btn btn-default" id="btn1" data-i18n="[value]resources.btn_startDraw"
               onclick="createHeatPoints()"/>&nbsp; &nbsp;
        <input type="button" class="btn btn-default" data-i18n="[value]resources.text_input_value_clear"
               onclick="clearHeatPoints()"/>
    </div>
</div>
<div class="row panel panel-primary" id="colorList">
    <div class="winTitle">
        <span class="title_left" data-i18n="resources.text_addedColor"></span>
        <span class="title_right"><a href="javascript:closeWindow()" title="关闭窗口" data-i18n="resources.text_close"></a></span>
    </div>
</div>
<div id="map"></div>
<script type="text/javascript" include="bootstrap,colorpicker,widgets.alert" src="../js/include-web.js"></script>
<script type="text/javascript" exclude="iclient-classic" src="../../dist/classic/include-classic.js"></script>
<script>
    var host = window.isLocal ? window.server : "http://support.supermap.com.cn:8090";
    var map, layer, heatMapLayer, color = [],
        url = host + "/iserver/services/map-world/rest/maps/World";

    init();

    function init() {
        if (!document.createElement('canvas').getContext) {
            widgets.alert.showAlert(resources.msg_supportCanvas, false);
            return;
        }

        map = new SuperMap.Map("map", {
            controls: [
                new SuperMap.Control.ScaleLine(),
                new SuperMap.Control.Zoom(),
                new SuperMap.Control.Navigation({
                    dragPanOptions: {
                        enableKinetic: true
                    }
                })]
        });
        map.addControl(new SuperMap.Control.MousePosition());
        layer = new SuperMap.Layer.TiledDynamicRESTLayer("World", url, {
            transparent: true,
            cacheEnabled: true
        }, {maxResolution: "auto"});
        heatMapLayer = new SuperMap.Layer.HeatMapLayer(
            "heatMap",
            {
                "radius": 45,
                "featureWeight": "value",
                "featureRadius": "geoRadius"
            }
        );
        heatMapLayer.colors = [];
        layer.events.on({"layerInitialized": addLayer});
    }

    function addLayer() {
        map.addLayers([layer, heatMapLayer]);
        map.setCenter(new SuperMap.LonLat(0, 0), 0);
    }

    function createHeatPoints() {
        widgets.alert.clearAlert();
        clearHeatPoints();
        var heatPoints = [];
        var num = parseInt(document.getElementById('heatNums').value);
        var radius = parseInt(document.getElementById('heatradius').value);
        var unit = document.getElementById("radiusUnit").value,
            useGeoRadius = false;
        if ("degree" == unit) {
            useGeoRadius = true;
        }
        heatMapLayer.radius = radius
        for (var i = 0; i < num; i++) {
            heatPoints[i] = new SuperMap.Feature.Vector(
                new SuperMap.Geometry.Point(
                    Math.random() * 360 - 180,
                    Math.random() * 180 - 90
                ),
                {
                    "value": Math.random() * 9,
                    "geoRadius": useGeoRadius ? radius : null
                }
            );
        }
        heatMapLayer.addFeatures(heatPoints);
    }

    function clearHeatPoints() {
        widgets.alert.clearAlert();
        heatMapLayer.removeAllFeatures();
    }

    $(document).ready(function () {
        $("#cp1").colorpicker({
            ishex: false,
            fillcolor: true,
            event: 'mouseover',
            target: $("#cp1text"),
            success: addColor

        });
        $("#_creset").css("font-size", "12px");
        $("#_creset").css("padding-right", "20px");
        $("#colorpanel").css({"z-index": "100"});

    });

    //添加颜色
    function addColor() {
        var severColor = document.getElementById("cp1text");
        if (severColor.value.match(/rgb()/)) {
            for (var i = 0; i < color.length; i++) {
                if (severColor.value == color[i]) {
                    widgets.alert.showAlert(resources.msg_repeatedAdditions, false);
                    return;
                    $("#colorList").css("display", "block");
                }
            }
            initColorList(severColor.value);
            color.push(severColor.value);
            var cl_s = "new SuperMap.REST.ServerColor" + severColor.value.replace(/rgb/gi, "");
            var cl = eval(cl_s);
            heatMapLayer.colors.push(cl);
            $("#colorList").css("display", "block");
        }
        else {
            widgets.alert.showAlert(resources.msg_chooseColor, true);
            severColor.value = "";
        }

    }

    function initColorList(value) {
        var colorList = $("#colorList");
        colorList.append(
            '<div ><button style="border-width:0px;margin:0px 5px 0px 0px;padding:0px;width:14px;height:14px;line-height: 15px;"' +
            'class="btn btn-danger" onclick="deleteColor(event)">-</button>' + '<span style="color: ' + value + '">' + value + '</span></div>'
        );

    }

    /*在颜色值列表里删除一个颜色*/
    function deleteColor(evt) {
        /*移除颜色*/
        var parent = evt.target.parentNode;
        parent.parentNode.removeChild(parent);

        //移除颜色里与上面的颜色文本对应的成员
        var len = color.length;
        len < 10;
        for (var i = 0; i < len; i++) {
            if (color[i] == parent.childNodes[1].innerHTML) {
                color.splice(i, 1);
                heatMapLayer.colors.splice(i, 1);
            }
        }
    }

    function closeWindow() {
        $("#colorList").css("display", "none");
    }

</script>

</body>
</html>